package com.trytech.mongoocrawler.client.common.queue;

import com.lmax.disruptor.WorkHandler;
import com.trytech.mongoocrawler.client.common.http.WebResult;
import com.trytech.mongoocrawler.client.common.util.MySQLUtils;
import com.trytech.mongoocrawler.client.entity.LianjiaItem;
import com.trytech.mongoocrawler.client.parser.LianjiaHtmlParser;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by hp on 2017-1-25.
 */
public class WebResultFetcherWorkHandler extends FetcherWorkHandler implements WorkHandler<WebResultFetcherEvent> {

    @Override
    public void onEvent(WebResultFetcherEvent webResultFetcherEvent) throws Exception {
        WebResult<String> webResult = webResultFetcherEvent.getData();
        if(webResult != null) {
            String html = webResult.getData();
            if(StringUtils.isNotEmpty(html)){
                try {
                    List<LianjiaItem> itemList = LianjiaHtmlParser.parse(html);
                    for(LianjiaItem item : itemList){
                        String sql = "insert into lj_house(`C_TITLE`,`C_LOCATION`,`C_TYPE`,`C_FLOORSPACE`,`C_PRICE`,`C_UNIT_PRICE`) values('" + item.getTitle() + "','" + item.getLocation() + "','"+item.getType()+
                                "','"+item.getFloorSpace()+"',"+item.getPrice().toString()+","+item.getUnitPrice().toString()+")";
                        MySQLUtils.insert(sql);
                    }

                }catch (SQLException ex){
                    ex.printStackTrace();
                }
            }
        }
    }
}
